/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.yami.shop.bean.app.dto.ExpressQueryDto;
import com.yami.shop.bean.param.ExpressQueryParam;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.Express100Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 快递查询接口
 *
 * @author AI Assistant
 */
@RestController
@RequestMapping("/p/express")
@Tag(name = "快递查询接口")
public class ExpressController {

    @Autowired
    private Express100Service express100Service;

    /**
     * 查询快递物流信息
     */
    @PostMapping("/query")
    @Operation(summary = "查询快递物流信息", description = "根据快递公司编码和快递单号查询物流信息")
    public ServerResponseEntity<ExpressQueryDto> query(@RequestBody ExpressQueryParam param) {
        ExpressQueryDto result = express100Service.queryExpress(param);
        return ServerResponseEntity.success(result);
    }

    /**
     * 查询快递物流信息（简化参数）
     */
    @GetMapping("/queryByNumber")
    @Operation(summary = "查询快递物流信息", description = "根据快递公司编码和快递单号查询物流信息")
    @Parameter(name = "companyCode", description = "快递公司编码", required = true)
    @Parameter(name = "expressNo", description = "快递单号", required = true)
    @Parameter(name = "phone", description = "手机号后四位（可选）", required = false)
    public ServerResponseEntity<ExpressQueryDto> queryByNumber(
            @RequestParam("companyCode") String companyCode,
            @RequestParam("expressNo") String expressNo,
            @RequestParam(value = "phone", required = false) String phone) {
        
        ExpressQueryDto result;
        if (phone != null && !phone.trim().isEmpty()) {
            result = express100Service.queryExpress(companyCode, expressNo, phone);
        } else {
            result = express100Service.queryExpress(companyCode, expressNo);
        }
        
        return ServerResponseEntity.success(result);
    }

    /**
     * 检查快递查询服务是否可用
     */
    @GetMapping("/checkService")
    @Operation(summary = "检查服务状态", description = "检查快递查询服务是否可用")
    public ServerResponseEntity<Boolean> checkService() {
        boolean isAvailable = express100Service.isServiceAvailable();
        return ServerResponseEntity.success(isAvailable);
    }
}